﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using NLBS.Entity;
using NHibernate.Criterion;

namespace NLBS.Data
{
    public partial class NHibernateDataProvider : DbProvider
    {
        #region comment

        public override int GetAllCommentCount()
        {
            return CurrentSession.CreateCriteria(typeof(Comment)).List().Count;
        }

        public override IPagedCollection<Comment> GetAllCommentByPage(int startIndex, int pageSize)
        {
            IList<Comment> list = CurrentSession.CreateCriteria(typeof(Comment)).
                SetFirstResult(startIndex * pageSize).SetMaxResults(pageSize).List<Comment>();

            IPagedCollection<Comment> lists = new PagedCollection<Comment>(list);
            lists.MaxItems = Convert.ToInt32(CurrentSession.CreateQuery("select count(*) from Comment as c").UniqueResult());

            return lists;
        }
        

        public override IList<Comment> GetCommentList()
        {
            IList<Comment> list = CurrentSession.CreateCriteria(typeof(Comment))
                .List<Comment>();

            return list;
        }

        public override IList<Comment> GetCommentListByArticleID(int articleID)
        {
            return CurrentSession
                .CreateQuery(@"from Comment as comm
                            where comm.LogID=:articleID
                            ORDER BY comm.PostTime DESC")
                        .SetInt32("articleID", articleID)
                        .List<Comment>();
        }

        public override IList<Comment> GetCommentListByUserID(int userID)
        {
            IList<Comment> list = CurrentSession.CreateCriteria(typeof(Comment))
                 .Add(Expression.Eq("AuthorID", userID)).
                 AddOrder(Order.Desc("PostTime")).List<Comment>();

            return list;
        }

        public override Comment GetCommentByID(int commID)
        {
            return CurrentSession.Get<Comment>(commID);
        }

        public override int InsertComment(Comment comm)
        {
            CurrentSession.Save(comm);
            CurrentSession.Flush();
            return 1;
           
        }

        public override void UpdateComment(Comment comm)
        {
            CurrentSession.Update(comm);
            CurrentSession.Flush();
        }

        public override void DeleteComment(int commID)
        {
            CurrentSession.Delete(GetCommentByID(commID));
            CurrentSession.Flush();
        }

        public override IPagedCollection<Comment> GetCommentBySearch(int startIndex, int pageSize, string prefix)
        {
//            StringBuilder sb = new StringBuilder();
//            string[] prefixArray = prefix.Split(new char[] { ' ' });
           
//            for (int i = 0; i < prefixArray.Length; i++)
//            {
//                if (i > 0)
//                {
//                    sb.Append("AND ");
//                }
//                sb.AppendFormat("comm_content LIKE '%{0}%'", prefixArray[i]);
//            }
//            //comm_content
//            DbCommand dbCommand = db.GetSqlStringCommand(string.Format(@"SELECT top {0} tComm.*, 
//            tLog.log_id,tLog.log_catID,tLog.log_title,tLog.log_authorID,tLog.log_mode 
//            FROM [blog_Article] tLog, [blog_Comment] tComm 
//            WHERE comm_id<=(select min(comm_id) 
//            from (select top {1} comm_id from blog_Comment order by comm_id desc) as T) AND {2} 
//            and tLog.log_id=tComm.log_id ORDER BY tComm.comm_postTime DESC;
//            ", startIndex, pageSize, sb.ToString()));
//            IDataReader dr = db.ExecuteReader(dbCommand);
            IPagedCollection<Comment> lists = new PagedCollection<Comment>();
            //lists.MaxItems = ExecuteScalar(string.Format("SELECT COUNT(comm_id) AS TotalRecords from blog_Comment where {2}", sb.ToString()));
            //Comment comment = null;
            //while (dr.Read())
            //{
            //    comment = CommonDataProvider.PopulateCommentFromIDataReader(dr);
            //    comment.Article = PopulateCommentArticleFromIDataReader(dr);
            //    lists.Add(comment);
            //}
            
            return lists;
        }

        public override IList<Comment> GetTopComment(int intCount)
        {
            if (intCount < 10)
            {
                intCount = 10;
            }
            IList<Comment> list = CurrentSession.CreateCriteria(typeof(Comment))
               .SetMaxResults(intCount)
               .List<Comment>();

            return list;
        }

        #endregion
    }
}
